home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / linuxcon.000 / linuxcon / linuxconf-1.6 / misc / sstring.c < prev    next >
C/C++ Source or Header  |  1996-08-03  |  4KB  |  239 lines

  1. #pragma implementation
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include "misc.h"
  5.  
  6. PUBLIC SSTRING::SSTRING()
  7. {
  8.     str = NULL;
  9.     maxsiz = 200;
  10. }
  11.  
  12. PUBLIC SSTRING::SSTRING (const char *_str)
  13. {
  14.     str = strdup(_str);
  15.     maxsiz = 200;        // Arbitrary maximum
  16. }
  17.  
  18. PUBLIC SSTRING::SSTRING (const char *_str, int _maxsiz)
  19. {
  20.     str = strdup(_str);
  21.     maxsiz = _maxsiz;
  22. }
  23.  
  24. PUBLIC SSTRING::SSTRING(const SSTRING &_str)
  25. {
  26.     str = strdup(_str.get());
  27.     maxsiz = _str.maxsiz;
  28. }
  29. PUBLIC SSTRING &SSTRING::operator = (const SSTRING &_str)
  30. {
  31.     setfrom (_str.get());
  32.     maxsiz = _str.maxsiz;
  33.     return *this;
  34. }
  35. PUBLIC SSTRING::~SSTRING()
  36. {
  37.     free (str);
  38. }
  39.  
  40. PUBLIC void SSTRING::copy (char *dst) const
  41. {
  42.     *dst = '\0';
  43.     if (str != NULL) strcpy (dst,str);
  44. }
  45. PUBLIC void SSTRING::copy (SSTRING &dst) const
  46. {
  47.     dst.setfrom (get());
  48. }
  49.  
  50. /*
  51.     Initialise the SSTRING from a value.
  52.     This value may be a substring of the current value of this SSTRING
  53. */
  54. PUBLIC VIRTUAL void SSTRING::setfrom (const char *src)
  55. {
  56.     char *newstr = NULL;
  57.     if (src != NULL) newstr = strdup(src);
  58.     free (str);
  59.     str = newstr;
  60. }
  61. PUBLIC void SSTRING::setfrom (const SSTRING &src)
  62. {
  63.     setfrom (src.get());
  64. }
  65.  
  66. /*
  67.     Initialise a SSTRING from a integer (convert to ASCII decimal)
  68. */
  69. PUBLIC void SSTRING::setfrom (int val)
  70. {
  71.     char bufval[20];
  72.     sprintf (bufval,"%d",val);
  73.     setfrom(bufval);
  74. }
  75. /*
  76.     Append a string to the current content.
  77. */
  78. PUBLIC void SSTRING::append (const char *app)
  79. {
  80.     const char *pt1 = get();
  81.     int len1 = strlen(pt1);
  82.     int len2 = strlen(app);
  83.     char *newstr = (char*)malloc(len1+len2+1);
  84.     if (newstr != NULL){
  85.         strcpy (newstr,pt1);
  86.         strcpy (newstr+len1,app);
  87.         free (str);
  88.         str = newstr;
  89.     }
  90. }
  91.  
  92. /*
  93.     Return the numerical value of the SSTRING
  94. */
  95. PUBLIC int SSTRING::getval() const
  96. {
  97.     return atoi(get());
  98. }
  99. /*
  100.     Return the length of the string.
  101. */
  102. PUBLIC int SSTRING::getlen() const
  103. {
  104.     return strlen (get());
  105. }
  106. /*
  107.     Get the string value. Will never return NULL (only an empty string).
  108. */
  109. PUBLIC const char *SSTRING::get() const
  110. {
  111.     char *ret = str;
  112.     if (str == NULL) ret = "";
  113.     return ret;
  114. }
  115.  
  116. /*
  117.     Perform a strcmp on the string and the other one.
  118. */
  119. PUBLIC int SSTRING::cmp (const char *other) const
  120. {
  121.     char *one = str;
  122.     if (one == NULL) one = "";
  123.     return strcmp(one,other);
  124. }
  125. /*
  126.     Perform a strncmp on the string and the other one.
  127. */
  128. PUBLIC int SSTRING::ncmp (const char *other, int len) const
  129. {
  130.     char *one = str;
  131.     if (one == NULL) one = "";
  132.     return strncmp(one,other,len);
  133. }
  134. /*
  135.     Perform a stricmp on the string and the other one.
  136. */
  137. PUBLIC int SSTRING::icmp (const char *other) const
  138. {
  139.     char *one = str;
  140.     if (one == NULL) one = "";
  141.     return stricmp(one,other);
  142. }
  143.  
  144. /*
  145.     Perform a stricmp on the string and the other one.
  146. */
  147. PUBLIC int SSTRING::icmp (const SSTRING &other) const
  148. {
  149.     return icmp(other.get());
  150. }
  151.  
  152. /*
  153.     Find the occurence of a character in the string.
  154. */
  155. PUBLIC const char *SSTRING::strchr(char carac)
  156. {
  157.     const char *ret = NULL;
  158.     if (str != NULL) ret = ::strchr(str,carac);
  159.     return ret;
  160. }
  161.  
  162. PUBLIC int SSTRING::cmp(const SSTRING &other) const
  163. {
  164.     return cmp(other.get());
  165. }
  166.  
  167. PUBLIC void SSTRING::setmaxsiz(int size)
  168. {
  169.     maxsiz = size;
  170. }
  171.  
  172. PUBLIC int SSTRING::getmaxsiz() const
  173. {
  174.     return maxsiz;
  175. }
  176.  
  177. /*
  178.     Return != 0 if the SSTRING is an empty string.
  179. */
  180. PUBLIC int SSTRING::is_empty() const
  181. {
  182.     int ret = 1;
  183.     if (str != NULL) ret = str[0] == '\0';
  184.     return ret;
  185. }
  186.  
  187. /*
  188.     Cut the white space at the end of the string
  189. */
  190. PUBLIC void SSTRING::strip_end()
  191. {
  192.     if (str != NULL) ::strip_end (str);
  193. }
  194.  
  195. /*
  196.     Extract a word (non white space) from a string.
  197.     Return a pointer to the end of the word in the line.
  198. */
  199. PUBLIC char *SSTRING::copyword (const char *line)
  200. {
  201.     char word[1000];
  202.     line = str_copyword(word,line);
  203.     setfrom (word);
  204.     return (char*)line;
  205. }
  206.  
  207. /*
  208.     Record the comment part of the CSSTRING
  209. */
  210. PUBLIC void CSSTRING::setcomment(const SSTRING &com)
  211. {
  212.     comment.setfrom (com);
  213. }
  214.  
  215. #ifdef TEST
  216.  
  217. static void test (const char *name, SSTRING &t, SSTRING c)
  218. {
  219.     printf ("%s = :%s:[%p] == :%s:[%p]\n",name,t.get(),t.get(),c.get(),c.get());
  220. }
  221.  
  222. #define P(x)    test (#x,x,x)
  223.  
  224. int main (int, char *[])
  225. {
  226.     SSTRING t1;
  227.     t1.setfrom ("allo");
  228.     P(t1);
  229.     SSTRING t2 (t1);
  230.     P(t2);
  231.     SSTRING t3;
  232.     t3 = t2;
  233.     P(t3);
  234.     return 0;
  235. }
  236.  
  237. #endif
  238.  
  239.